package it.polimi.travelDream.ejb.servlet;

import it.polimi.travelDream.ejb.interfaces.HotelMgrRemote;

import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Map;

import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class EditHotelServlet
 */
@WebServlet("/EditHotelServlet")
public class EditHotelServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	DateFormat format = new SimpleDateFormat("dd/MM/yyyy");
	@EJB
	HotelMgrRemote hmr;
	
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public EditHotelServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Map<String, String[]> mappa = request.getParameterMap();
		format.setLenient(false);
		if (mappa.get("name")[0].isEmpty() || mappa.get("address")[0].isEmpty() || mappa.get("city")[0].isEmpty() || mappa.get("availability")[0].isEmpty() || mappa.get("typeRum")[0].isEmpty() ||	mappa.get("costPerDay")[0].isEmpty())
		{
			request.setAttribute("message","Empty fields.");
			request.getRequestDispatcher("/BPEditHotel.jsp").forward(request, response); 
		}
		else
		{
			 try {Integer.parseInt(mappa.get("availability")[0]);
				 format.parse(mappa.get("availability")[0]);	
		String id=mappa.get("id")[0];
		
		String name=mappa.get("name")[0];
		
		String address=mappa.get("address")[0];
		
		String city=mappa.get("city")[0];
		
		String availability=mappa.get("availability")[0];
		
		String typeRum=mappa.get("typeRum")[0];
		
		String costPerDay=mappa.get("costPerDay")[0];
		
		hmr.modifyHotel(id, name, address, city, availability, typeRum, costPerDay);
		
			 } catch (ParseException e) {
				 
				 request.setAttribute("message","Date must be dd/mm/yyyy");
					request.getRequestDispatcher("/BPEditHotel.jsp").forward(request, response); 
				 
			 }
		
		
		
		
		
		
		
		
		
	}

	}}
